package com.junzhiit.nengji.shop.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.shop.dao.AddressDAO;
import com.junzhiit.nengji.shop.model.Address;
import com.junzhiit.nengji.shop.model.enums.AreaLevel;
import com.junzhiit.nengji.shop.model.enums.Is;

@Repository
public class AddressDAOImpl extends AbstractBaseDAO<Address> implements AddressDAO {

	@Override
	public List<Address> findAll(String userId) {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from address where is_delete=?");

		StatementParameter params = new StatementParameter();
		params.setInt(IsDelete.NO.toIntValue());

		if (StringUtils.isNotBlank(userId)) {
			sql.append(" and user_id=?");
			params.setString(userId);
		}
		sql.append(" order by is_checked desc");
		return queryForList(sql.toString(), Address.class, params);
	}
	
	@Override
	public Address getAddressById(String id, String userId) {
		StringBuilder sql = new StringBuilder();
		sql.append("select * from address where id = ? and user_id = ? and is_delete = ?");
		StatementParameter params = new StatementParameter();
		params.setString(id);
		params.setString(userId);
		params.setInt(IsDelete.NO.toIntValue());
		return super.query(sql.toString(),Address.class, params);
	}

	@Override
	public int updateStatus(Address address) {
		StringBuilder sql = new StringBuilder();
		sql.append("update address set is_checked = ? where user_id = ?");
		StatementParameter params = new StatementParameter();
		params.setInt(address.getIsChecked());
		params.setString(address.getUserId());
		return super.updateForRecord(sql.toString(), params);
	}
	
	@Override
	public int updateById(Address address) {
		StringBuilder sql = new StringBuilder();
		sql.append("update address set is_checked = ? where id = ?");
		StatementParameter params = new StatementParameter();
		params.setInt(address.getIsChecked());
		params.setString(address.getId());
		return super.updateForRecord(sql.toString(), params);
	}

	@Override
	public int countAddress(String userId) {
		StringBuilder sql = new StringBuilder();
		sql.append("select count(user_id) from address where user_id = ? and is_delete = ?");
		StatementParameter params = new StatementParameter();
		params.setString(userId);
		params.setInt(IsDelete.NO.toIntValue());
		return super.queryForInt(sql.toString(),params);
	}

	@Override
	public Address getUserAddress(String userId) {
		String sql = " select * from address where user_id =? and is_delete = ? order by created_time desc limit 1 ";
		StatementParameter params = new StatementParameter();
		params.setString(userId);
		params.setInt(IsDelete.NO.toIntValue());
		return super.query(sql,Address.class,params);
	}
	
	@Override
	public Address getDefalutAddress(String userId) {
		StringBuilder sql = new StringBuilder();
		sql.append("select * from address where user_id = ? and is_checked = ? and is_delete = ?");
		StatementParameter params = new StatementParameter();
		params.setString(userId);
		params.setInt(Is.YES.toIntValue());
		params.setInt(IsDelete.NO.toIntValue());
		return super.query(sql.toString(),Address.class, params);
	}

	@Override
	public boolean update(String address, String addressId) {
		String sql = "update address ad set area_code = ( select city_id from area  ar where  ar.`level` = ? and ar.merger_name = ? ) where ad.id = ?";
		StatementParameter param = new StatementParameter();
		
		param.setInt(AreaLevel.TWO.toIntValue());
		param.setString(address);
		param.setString(addressId);
		
		return super.updateForBoolean(sql, param);
	}
}
